package com.ruoyi.paipan.common;

import com.ruoyi.paipan.utils.FotoUtil;
import com.ruoyi.paipan.utils.LunarUtil;

import java.util.ArrayList;
import java.util.List;

public class Foto {
    public static int DEAD_YEAR = -543;
    private Lunar lunar;

    public Foto(Lunar lunar) {
        this.lunar = lunar;
    }


    public static Foto fromLunar(Lunar lunar)
    {
        return new Foto(lunar);
    }

    public static Foto fromYmdHms(int year, int month, int day, int hour, int minute, int second)
    {
        return Foto.fromLunar(Lunar.fromYmdHms(year + Foto.DEAD_YEAR - 1, month, day, hour, minute, second));
    }

    public static Foto fromYmd(int year, int month, int day)
    {
        return Foto.fromYmdHms(year, month, day, 0, 0, 0);
    }

    public Lunar getLunar()
    {
        return this.lunar;
    }

    public int getYear()
    {
        int sy = this.lunar.getSolar().getYear();
        int y = sy - Foto.DEAD_YEAR;
        if (sy == this.lunar.getYear()) {
            y++;
        }
        return y;
    }

    public int getMonth()
    {
        return this.lunar.getMonth();
    }

    public int getDay()
    {
        return this.lunar.getDay();
    }

    public StringBuilder getYearInChinese()
    {
        String y = this.getYear() + "";
        StringBuilder s = new StringBuilder();
        for (int i = 0, j = y.length(); i < j; i++) {
            s.append(LunarUtil.NUMBER.get(Character.codePointAt(y.substring(i,i+1),0) - 48));
        }
        return s;
    }

    public String getMonthInChinese()
    {
        return this.lunar.getMonthInChinese();
    }

    public String getDayInChinese()
    {
        return this.lunar.getDayInChinese();
    }

    public List<FotoFestival> getFestivals()
    {
        return FotoUtil.getFestivals(Math.abs(this.getMonth()) + "-" + this.getDay());
    }

    public List<String> getOtherFestivals()
    {
        List<String> l = new ArrayList<>();
        String key = this.getMonth() + "-" + this.getDay();
        if (!FotoUtil.OTHER_FESTIVAL.get(key).isEmpty()) {
            l.addAll(FotoUtil.OTHER_FESTIVAL.get(key));
        }
        return l;
    }

    public boolean isMonthZhai()
    {
        int m = this.getMonth();
        return 1 == m || 5 == m || 9 == m;
    }

    public boolean isDayYangGong()
    {
        for (FotoFestival f : this.getFestivals()) {
            if("杨公忌".compareTo(f.getName()) == 0){
                return  true;
            }
        }
        return false;
    }

    public boolean isDayZhaiShuoWang()
    {
        int d = this.getDay();
        return 1 == d || 15 == d;
    }

    public boolean isDayZhaiSix()
    {
        int d = this.getDay();
        if (8 == d || 14 == d || 15 == d || 23 == d || 29 == d || 30 == d) {
            return true;
        } else if (28 == d) {
            LunarMonth m = LunarMonth.fromYm(this.lunar.getYear(), this.getMonth());
            return null != m && 30 != m.getDayCount();
        }
        return false;
    }

    public boolean isDayZhaiTen()
    {
        int d = this.getDay();
        return 1 == d || 8 == d || 14 == d || 15 == d || 18 == d || 23 == d || 24 == d || 28 == d || 29 == d || 30 == d;
    }

    public boolean isDayZhaiGuanYin()
    {
        String k = this.getMonth() + "-" + this.getDay();
        for (String d : FotoUtil.DAY_ZHAI_GUAN_YIN) {
            if(k.compareTo(d) == 0){
                return true;
            }
        }
        return false;
    }

    public String getXiu()
    {
        return FotoUtil.getXiu(String.valueOf(this.getMonth()), this.getDay());
    }

    public String getXiuLuck()
    {
        return LunarUtil.XIU_LUCK.get(this.getXiu());
    }

    public String getXiuSong()
    {
        return LunarUtil.XIU_SONG.get(this.getXiu());
    }

    public String getZheng()
    {
        return LunarUtil.ZHENG.get(this.getXiu());
    }

    public String getAnimal()
    {
        return LunarUtil.ANIMAL.get(this.getXiu());
    }

    public String getGong()
    {
        return LunarUtil.GONG.get(this.getXiu());
    }

    public String getShou()
    {
        return LunarUtil.SHOU.get(this.getGong());
    }

    @Override
    public String toString()
    {
        return String.format("%s年%s月%s", this.getYearInChinese(), this.getMonthInChinese(), this.getDayInChinese());
    }

    public String toFullString()
    {
        StringBuilder s = new StringBuilder(this.toString());
        for (FotoFestival f : this.getFestivals()) {
            s.append(" (").append(f).append(")");
        }
        return s.toString();
    }
}
